package entity;

import java.awt.*;

/**
 * 玩家飞机类
 */
public class Player extends FlyingObject {
    private int speed = 10; // 移动速度
    private int health = 100; // 生命值
    private int maxHealth = 100; // 最大生命值
    private boolean alive = true; // 是否存活
    private long deathTime = 0; // 死亡时间戳
    
    public Player(int x, int y, Image image) {
        this.x = x;
        this.y = y;
        this.image = image;
        this.width = image.getWidth(null);
        this.height = image.getHeight(null);
    }
    
    @Override
    public void move() {
        // 玩家飞机由键盘控制，不需要自动移动
    }
    
    /**
     * 玩家移动方法
     * @param dx x轴偏移量
     * @param dy y轴偏移量
     */
    public void move(int dx, int dy) {
        this.x += dx;
        this.y += dy;
    }
    
    @Override
    public boolean isOutOfBounds(int panelWidth, int panelHeight) {
        // 玩家飞机不能超出边界
        if (x < 0) x = 0;
        if (x > panelWidth - width) x = panelWidth - width;
        if (y < 0) y = 0;
        if (y > panelHeight - height) y = panelHeight - height;
        return false;
    }
    
    @Override
    public boolean isAlive() {
        return alive && health > 0;
    }
    
    /**
     * 发射子弹
     * @param image 子弹图片
     * @return 子弹对象
     */
    public Bullet shoot(Image image) {
        int x = this.x + this.width / 2 - image.getWidth(null) / 2;
        int y = this.y - image.getHeight(null);
        return new Bullet(x, y, image, -10); // 向上发射
    }
    
    /**
     * 扣除生命值
     * @param damage 伤害值
     */
    public void takeDamage(int damage) {
        health -= damage;
        if (health <= 0) {
            health = 0;
            alive = false;
            deathTime = System.currentTimeMillis();
        }
    }
    
    /**
     * 恢复生命值
     * @param heal 恢复值
     */
    public void heal(int heal) {
        health += heal;
        if (health > maxHealth) {
            health = maxHealth;
        }
    }
    
    // getter和setter方法
    public int getSpeed() {
        return speed;
    }
    
    public void setSpeed(int speed) {
        this.speed = speed;
    }
    
    public int getHealth() {
        return health;
    }
    
    public void setHealth(int health) {
        this.health = health;
        if (this.health <= 0) {
            this.health = 0;
            alive = false;
            deathTime = System.currentTimeMillis();
        }
    }
    
    public int getMaxHealth() {
        return maxHealth;
    }
    
    public void setMaxHealth(int maxHealth) {
        this.maxHealth = maxHealth;
    }
    
    public long getDeathTime() {
        return deathTime;
    }
}